package com.xblibo.xruth.common;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xblibo.xruth.exception.OtherException;

/**
 * <p>Title: BaseServiceImpl.java </p>
 * <p>Description:  </p>
 * <p>Copyright: 易宝 2011*版权所有 </p>
 * <p>Company: yeepay </p>
 * @author xblibo
 * @version 1.0 ,  2011  下午10:13:44
 */
public class BaseServiceImpl<T,PK extends Serializable> implements BaseService<T,PK> {
	
	
	protected BaseDao<T,PK> baseDao;

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor={RuntimeException.class})
	public void delete(T entity) throws OtherException {
		baseDao.delete(entity);
	}
	
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor={RuntimeException.class})
	public void delete(PK id) throws OtherException {
		baseDao.delete(id);
	}
	
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor={RuntimeException.class})
	public void deleteAll(Collection<T> entitys) throws OtherException {
		baseDao.deleteAll(entitys);
	}

	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public <X> List<X> find(String hql, Object... values) throws OtherException {
		return baseDao.find(hql, values);
	}

	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public <X> List<X> find(String hql, Map<String, ?> values)
			throws OtherException {
		return baseDao.find(hql, values);
	}
	
	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public List<T> findAll() throws OtherException {
		return baseDao.findAll();
	}
	
	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public <X> List<X> findBy(String propertyName, Object value)
			throws OtherException {
		return baseDao.findBy(propertyName, value);
	}

	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public T findById(PK id) throws OtherException {
		return baseDao.findById(id);
	}

	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public List<T> findByIds(List<PK> ids) throws OtherException {
		return baseDao.findByIds(ids);
	}

	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public <X> X findUnique(String hql, Map<String, ?> values)
			throws OtherException {
		return baseDao.findUnique(hql, values);
	}
	
	@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
	public T findUniqueBy(String propertyName, Object value)
			throws OtherException {
		return baseDao.findUniqueBy(propertyName, value);
	}

	public void saveOrUpdate(T entity) throws OtherException {
		baseDao.saveOrUpdate(entity);
	}

	public void saveOrUpdateAll(Collection<T> entitys) throws OtherException {
		baseDao.saveOrUpdateAll(entitys);
	}

	public BaseDao<T, PK> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao<T, PK> baseDao) {
		this.baseDao = baseDao;
	}
	
	
}
